package com.cbz.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cbz.example.entity.User;
import com.cbz.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 创建用户
    @PostMapping
    public boolean createUser(@RequestBody User user) {
        return userService.save(user);
    }

    // 根据 ID 查询用户
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getById(id);
    }

    // 查询所有用户
    @GetMapping
    public List<User> getAllUsers() {
        return userService.list();
    }

    // 分页查询用户
    @GetMapping("/page/{current}/{size}")
    public Page<User> getUsersByPage(@PathVariable long current, @PathVariable long size) {
        Page<User> page = new Page<>(current, size);
        return userService.page(page);
    }

    // 更新用户信息
    @PutMapping
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    // 根据 ID 删除用户
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable Long id) {
        return userService.removeById(id);
    }
}